﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using FBEntities.FBObjects.User;
using FBEntities.FBObjects;

namespace XFControls.XFPanels.XFPanelItems
{
    public class XFItemFriend : IXFItem
    {
        #region IXFItem Members

        public XFItemType ItemType { get; set; }
        public XFItemState State { get; set; }
        public XFPanelBase Parent { get; set; }
        public string Name { get; set; }
        public string status { get; set; }
        public FBProxyPerson Friend { get; set; }
        public Bitmap Buffer { get; set; }
        public XFItemStyle Style { get; set; }

        private string _name;
        private int height = 60;

        public XFItemFriend()
        {
            Style = new XFItemStyle()
            {
                DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash,
                TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold),
                SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 6, FontStyle.Regular),
                SelectedBackgroundColor = Color.Black,
                SelectedTextColor = Color.White,
                SelectedSecondaryTextColor = Color.AliceBlue,
                TextColor = Color.Black,
                SecondaryTextColor = Color.DimGray,
                Padding = 5
            };

            ItemType = XFItemType.Clickable;
        }

        public int GetHeight()
        {
            return height;
        }

        public void ResetHeight()
        {
        }

        public void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            int width = Screen.PrimaryScreen.WorkingArea.Width;

            if (Parent != null)
            {
                width -= Parent.AlphaBarWidth;

                if (Parent.ScrollbarVisible)
                    width -= 5;
            }

            XFControlUtils.DrawBoarders(Style, g, x, y, width, height);

            if (State == XFItemState.Selected)
            {
                XFControlUtils.DrawBackgroundSelected(Style, g, x, y, width, height);

                this.DrawIcon(Friend.Picture.url, g, new Rectangle(Style.PaddingLeft, Style.PaddingTop, 50, 50));

                if (string.IsNullOrEmpty(_name))
                    _name = XFControlUtils.EllipsisWord(width, Name, Style.TextFont);

                g.DrawString(_name, Style.TextFont, new SolidBrush(Style.SelectedTextColor), x + 60, y + 5);

                if (!string.IsNullOrEmpty(status))
                {
                    g.DrawString(status, Style.SecondaryTextFont, new SolidBrush(Style.SelectedSecondaryTextColor), x + 60, y + 30);
                }
            }
            else
            {
                XFControlUtils.DrawBackground(Style, g, x, y, width, height);

                this.DrawIcon(Friend.Picture.url, g, new Rectangle(Style.PaddingLeft, Style.PaddingTop, 50, 50));

                if (string.IsNullOrEmpty(_name))
                    _name = XFControlUtils.EllipsisWord(width, Name, Style.TextFont);

                g.DrawString(_name, Style.TextFont, new SolidBrush(Style.TextColor), x + 60, y + 5);

                if (!string.IsNullOrEmpty(status))
                {
                    g.DrawString(status, Style.SecondaryTextFont, new SolidBrush(Style.SecondaryTextColor), x + 60, y + 30);
                }
            }
        }

        public XFItemClickResult GetClickResult()
        {
            if (Parent == null)
                return XFItemClickResult.Nothing;

            var friendProf = Friend.ToPerson();
            //friendProf.Parse();

            var rp = XFItemControlUtils.SetupProfile(friendProf);
            rp.PanelName = Friend.name;

            var back = new XFItemBack();

            rp.AddHeaderItem(back);
            Parent.SetRightPanel(rp);

            rp.ShowAlphaBar(false);
            rp.ShowScrollbar(true);

            return XFItemClickResult.SlideLeft;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (Buffer != null)
                Buffer.Dispose();
            if (Style != null)
                Style.Dispose();

            Style = null;
            Buffer = null;

        }

        #endregion
    }
}
